package com.sc.nft.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.MallBillDao;
import com.sc.nft.entity.MallBill;
import com.sc.nft.entity.dto.FinanceFileDTO;
import com.sc.nft.entity.dto.MallBillAdminPageDTO;
import com.sc.nft.entity.dto.MallOrderBillAdminExcelDTO;
import com.sc.nft.entity.dto.MallOrderBillAdminPageDTO;
import com.sc.nft.entity.vo.AuditBillVO;
import com.sc.nft.entity.vo.MallBillAdminPageVO;
import com.sc.nft.entity.vo.MallOrderBillAdminPageVO;
import com.sc.nft.enums.AuditMallBillStatusEnum;
import com.sc.nft.enums.MallBillStatusEnum;
import com.sc.nft.enums.wallet.MallDaoWalletTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.MallBillService;
import com.sc.nft.service.MallBillStatusLogService;
import com.sc.nft.service.MallDaoWalletService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

/**
 * <p>
 * 商城对账单 服务实现类
 * </p>
 *
 * @author ligen
 * @since 2024-01-17
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MallBillServiceImpl implements MallBillService {

    private final MallBillDao mallBillDao;
    private final MallBillStatusLogService mallBillStatusLogService;
    private final MallDaoWalletService mallDaoWalletService;

    @Override
    public MallBill getById(Long id) {
        return mallBillDao.getById(id);
    }

    /**
     * 获取对账单列表
     *
     * @param mallBillAdminPageVO
     * @return
     */
    @Override
    public Page<MallBillAdminPageDTO> getMallBillAdminPageDTO(MallBillAdminPageVO mallBillAdminPageVO) {
        return mallBillDao.getMallBillAdminPageDTO(mallBillAdminPageVO.getPageNo(), mallBillAdminPageVO.getPageSize(), mallBillAdminPageVO.getDaoId(), mallBillAdminPageVO.getStartTime(), mallBillAdminPageVO.getEndTime(), mallBillAdminPageVO.getSettlementNo(), mallBillAdminPageVO.getStatus());
    }

    /**
     * 获取对账单详情
     *
     * @param id
     * @return
     */
    @Override
    public MallBillAdminPageDTO getDetailById(Long id) {
        MallBillAdminPageDTO detailById = mallBillDao.getDetailById(id);
        if (Objects.nonNull(detailById) && !StringUtils.isEmpty(detailById.getInvoiceJson())) {
            detailById.setInvoice(JSONArray.parseArray(detailById.getInvoiceJson(), FinanceFileDTO.class));
        }
        return detailById;
    }

    /**
     * 获取对账单订单
     *
     * @param mallOrderBillAdminPageVO
     * @return
     */
    @Override
    public Page<MallOrderBillAdminPageDTO> getMallOrderBillAdminPageDTOByBillId(MallOrderBillAdminPageVO mallOrderBillAdminPageVO) {
        return mallBillDao.getMallOrderBillAdminPageDTOByBillId(mallOrderBillAdminPageVO.getPageNo(), mallOrderBillAdminPageVO.getPageSize(), mallOrderBillAdminPageVO.getBillId(), mallOrderBillAdminPageVO.getProductName());
    }

    /**
     * 获取导出EXCEL对账单订单
     *
     * @param mallOrderBillAdminPageVO
     * @return
     */
    @Override
    public Page<MallOrderBillAdminExcelDTO> getMallOrderBillAdminExcelDTOByBillId(MallOrderBillAdminPageVO mallOrderBillAdminPageVO) {
        return mallBillDao.getMallOrderBillAdminExcelDTOByBillId(mallOrderBillAdminPageVO.getPageNo(), mallOrderBillAdminPageVO.getPageSize(), mallOrderBillAdminPageVO.getBillId());
    }

    /**
     * 修改对账单状态
     *
     * @param id
     * @param statusEnum
     * @return
     */
    @Override
    public void updateStatus(Long id, MallBillStatusEnum statusEnum, String remark, Date billTime) {
        mallBillDao.updateStatus(id, statusEnum, remark, billTime);
        // 创建日志
        mallBillStatusLogService.create(id, statusEnum, statusEnum.getDescription());
    }

    /**
     * 账单审核
     *
     * @param auditBillVO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(AuditBillVO auditBillVO) {
        MallBill mallBill = getById(auditBillVO.getId());
        if (Objects.isNull(mallBill)) {
            throw new GlobalRunTimeException("对账单不存在");
        }
        if (AuditMallBillStatusEnum.DECLINED == auditBillVO.getStatus()) {
            // 拒绝
            if (MallBillStatusEnum.CHECKING_ACCOUNTS == mallBill.getStatus()) {
                // 对账中
                updateStatus(mallBill.getId(), MallBillStatusEnum.DECLINED, auditBillVO.getRemark(), null);
            }
        } else if (AuditMallBillStatusEnum.SUCCESSFUL_RECONCILIATION == auditBillVO.getStatus()) {
            // 对账成功，待开票
            if (MallBillStatusEnum.CHECKING_ACCOUNTS == mallBill.getStatus()) {
                // 对账中
                updateStatus(mallBill.getId(), MallBillStatusEnum.SUCCESSFUL_RECONCILIATION, auditBillVO.getRemark(), null);
            }
        } else if (AuditMallBillStatusEnum.FINANCIAL_AUDIT == auditBillVO.getStatus()) {
            // 财务审核中
            if (MallBillStatusEnum.SUCCESSFUL_RECONCILIATION == mallBill.getStatus()) {
                //  对账成功，待开票
                updateStatus(mallBill.getId(), MallBillStatusEnum.FINANCIAL_AUDIT, auditBillVO.getRemark(), null);
            }
        } else if (AuditMallBillStatusEnum.DONE == auditBillVO.getStatus()) {
            // 已完成
            if (MallBillStatusEnum.FINANCIAL_AUDIT == mallBill.getStatus()) {
                //  财务审核中
                updateStatus(mallBill.getId(), MallBillStatusEnum.DONE, auditBillVO.getRemark(), new Date());
                // 审核完成后 需要到dao账户
                mallDaoWalletService.addBalance(mallBill.getDaoId(),mallBill.getBillAmount(),mallBill.getBillActivityAmount(), BigDecimal.ZERO, MallDaoWalletTypeEnum.MALL_ORDER_SETTLEMENT,MallDaoWalletTypeEnum.MALL_ORDER_SETTLEMENT.getDescription());
            }
        } else if (AuditMallBillStatusEnum.FINANCIAL_REJECTION == auditBillVO.getStatus()) {
            // 财务拒绝
            // 拒绝
            if (MallBillStatusEnum.FINANCIAL_AUDIT == mallBill.getStatus()) {
                // 对账中
                updateStatus(mallBill.getId(), MallBillStatusEnum.FINANCIAL_REJECTION, auditBillVO.getRemark(), null);
            }
        } else if (AuditMallBillStatusEnum.FINANCIAL_RETURN == auditBillVO.getStatus()) {
            // 财务退回
            if (MallBillStatusEnum.FINANCIAL_AUDIT == mallBill.getStatus()) {
                // 财务审核中  财务退回 到对账成功 待开票
                updateStatus(mallBill.getId(), MallBillStatusEnum.SUCCESSFUL_RECONCILIATION, auditBillVO.getRemark(), null);
            }
        }
    }


}
